import { assign } from "min-dash";

/**
 * A palette provider for BPMN 2.0 elements.
 */
export default function PaletteProvider(palette, create, elementFactory, spaceTool, lassoTool, handTool, globalConnect, translate) {
  this._palette = palette;
  this._create = create;
  this._elementFactory = elementFactory;
  this._spaceTool = spaceTool;
  this._lassoTool = lassoTool;
  this._handTool = handTool;
  this._globalConnect = globalConnect;
  this._translate = translate;

  palette.registerProvider(this);
}

PaletteProvider.$inject = ["palette", "create", "elementFactory", "spaceTool", "lassoTool", "handTool", "globalConnect", "translate"];

PaletteProvider.prototype.getPaletteEntries = function() {
  var actions = {}
  var create = this._create
  var elementFactory = this._elementFactory
  var spaceTool = this._spaceTool
  var lassoTool = this._lassoTool
  var handTool = this._handTool
  var globalConnect = this._globalConnect
  var translate = this._translate

  /**
   * 创建工具栏元素
   */
  function createAction(type, group, className, title, options) {
    function createListener(event) {
      var shape = elementFactory.createShape(assign({ type: type }, options));

      if (options) {
        shape.businessObject.di.isExpanded = options.isExpanded;
      }

      create.start(event, shape);
    }

    var shortType = type.replace(/^bpmn:/, "");

    return {
      group: group,
      className: className,
      title: title || translate("Create {type}", { type: shortType }),
      action: {
        dragstart: createListener,
        click: createListener
      }
    };
  }

  assign(actions, {
    //  激活抓手工具
    "hand-tool": { group: "tools", className: "bpmn-icon-hand-tool", title: translate("Activate the hand tool"), action: { click: function(event) { handTool.activateHand(event); } } },
    //  激活套索工具
    "lasso-tool": { group: "tools", className: "bpmn-icon-lasso-tool", title: translate("Activate the lasso tool"), action: { click: function(event) { lassoTool.activateSelection(event); } } },
    //  激活创建/删除空间工具
    "space-tool": { group: "tools", className: "bpmn-icon-space-tool", title: translate("Activate the create/remove space tool"), action: { click: function(event) { spaceTool.activateSelection(event); } } },
    //  激活全局连接工具
    "global-connect-tool": { group: "tools", className: "bpmn-icon-connection-multi", title: translate("Activate the global connect tool"), action: { click: function(event) { globalConnect.toggle(event); } } },
    //  工具分割线
    "tool-separator": { group: "tools", separator: true },
    //  创建开始事件
    "create.start-event": createAction("bpmn:StartEvent", "event", "bpmn-icon-start-event-none", translate("Create StartEvent")),
    //  创建中间抛出事件/边界事件
    "create.intermediate-event": createAction( "bpmn:IntermediateThrowEvent", "event", "bpmn-icon-intermediate-event-none", translate("Create Intermediate/Boundary Event") ),
    //  创建结束事件
    "create.end-event": createAction("bpmn:EndEvent", "event", "bpmn-icon-end-event-none", translate("Create EndEvent")),
    //  创建网关
    "create.exclusive-gateway": createAction("bpmn:ExclusiveGateway", "gateway", "bpmn-icon-gateway-none", translate("Create Gateway")),
    //  创建用户任务
    "create.user-task": createAction("bpmn:UserTask", "activity", "bpmn-icon-user-task", translate("Create User Task")),
  });

  return actions;
};
